// pages/result/result.js
const app = getApp();

Page({
    data: {
        resultData: null
    },

    onLoad(options) {
        // 从参数中获取查询结果数据
        if (options.data) {
            try {
                const data = JSON.parse(decodeURIComponent(options.data));
                this.setData({
                    resultData: data
                });
                console.log('查询结果:', data);
            } catch (e) {
                console.error('解析数据失败:', e);
                wx.showToast({
                    title: '数据加载失败',
                    icon: 'none'
                });
            }
        }
    },

    // 导航到公交站
    navigateToStation(e) {
        const station = e.currentTarget.dataset.station;

        wx.openLocation({
            latitude: station.latitude,
            longitude: station.longitude,
            name: station.station_name,
            address: station.address,
            scale: 18,
            success: () => {
                console.log('打开地图成功');
            },
            fail: () => {
                wx.showToast({
                    title: '打开地图失败',
                    icon: 'none'
                });
            }
        });
    },

    // 分享公交站信息
    shareStation(e) {
        const station = e.currentTarget.dataset.station;

        const shareText = `📍 ${station.station_name}\n` +
            `🚌 线路: ${station.bus_lines.join('、')}\n` +
            `📏 距离: ${station.distance}米\n` +
            `📌 地址: ${station.address}`;

        wx.setClipboardData({
            data: shareText,
            success: () => {
                wx.showToast({
                    title: '信息已复制到剪贴板',
                    icon: 'success'
                });
            },
            fail: () => {
                wx.showToast({
                    title: '复制失败',
                    icon: 'none'
                });
            }
        });
    },

    // 打开地图显示当前位置和公交站
    openMap(e) {
        const station = e.currentTarget.dataset.station;
        const currentLocation = this.data.resultData.current_location;

        // 准备地图点位数据
        const points = [
            {
                latitude: currentLocation.latitude,
                longitude: currentLocation.longitude,
                name: '我的位置',
                desc: '当前位置',
                iconPath: '/images/location.png', // 需要添加图标文件
                id: 0
            },
            {
                latitude: station.latitude,
                longitude: station.longitude,
                name: station.station_name,
                desc: station.address,
                iconPath: '/images/bus.png', // 需要添加图标文件
                id: 1
            }
        ];

        wx.navigateTo({
            url: `/pages/map/map?points=${encodeURIComponent(JSON.stringify(points))}`
        });
    },

    // 重新查询
    refresh() {
        wx.navigateBack();
    },

    // 返回首页
    goBack() {
        wx.navigateBack();
    },

    // 页面分享配置
    onShareAppMessage() {
        const stationCount = this.data.resultData?.nearby_stations?.length || 0;
        return {
            title: `找到${stationCount}个附近公交站`,
            path: '/pages/index/index',
            imageUrl: '/images/share.png' // 需要添加分享图片
        };
    },

    // 分享到朋友圈
    onShareTimeline() {
        const stationCount = this.data.resultData?.nearby_stations?.length || 0;
        return {
            title: `🚌 公交站查询 - 找到${stationCount}个附近公交站`,
            imageUrl: '/images/share.png'
        };
    }
});
